package com.dayuanit.dy15.lesson28.demo5;

/**
 * 车站
 */
public class Station {

    //定义一个对象 当做一把锁
    private static final Object lock = new Object();

    //一共多少张票
    private static int ticketCount;

    public Station(int ticketCount) {
        this.ticketCount = ticketCount;
    }

    /**
     * 卖票的方法  此方法有问题，在多线程的情况下，会出现多卖的情况
     */
    public void sellTikct() {

        //让当前线程睡一会~
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (ticketCount == 0) {
            System.out.println("没票啦！");
            return;
        }

        Ticket ticket = new Ticket(ticketCount);
        ticketCount --;
        System.out.println(Thread.currentThread().getName() + ticket);
    }

    //    Thread-3票号:85 -- 85这张票重复消费了，这就是资源竞争的情况下，多线程的问题。
//    Thread-23票号:81
//    Thread-19票号:82
//    Thread-11票号:84
//    Thread-15票号:83
//    Thread-7票号:85


    //非静态方法 synchronized锁的是当前对象this
    public synchronized void sellTikct2() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (ticketCount == 0) {
            System.out.println("没票啦！");
            return;
        }

        Ticket ticket = new Ticket(ticketCount);
        ticketCount --;
        System.out.println(Thread.currentThread().getName() + ticket);
    }


    public void sellTikct3() {
        //同步代码块锁的是对象，主要某个线程持有这个对象的锁，那么就可以进到同步代码块中。
        synchronized (this) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (ticketCount == 0) {
                System.out.println("没票啦！");
                return;
            }

            Ticket ticket = new Ticket(ticketCount);
            ticketCount --;
            System.out.println(Thread.currentThread().getName() + ticket);
        }
    }


    public void sellTikct4() {
        //同步代码块锁的是对象，主要某个线程持有这个对象的锁，那么就可以进到同步代码块中。
        synchronized (lock) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (ticketCount == 0) {
                System.out.println("没票啦！");
                return;
            }

            Ticket ticket = new Ticket(ticketCount);
            ticketCount --;
            System.out.println(Thread.currentThread().getName() + ticket);
        }
    }

    //如果当前方法是静态方法，那么synchronized锁的是当前类对象:Station.class
    public synchronized static void sellTikct5() {
        //同步代码块锁的是对象，主要某个线程持有这个对象的锁，那么就可以进到同步代码块中。
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (ticketCount == 0) {
            System.out.println("没票啦！");
            return;
        }

        Ticket ticket = new Ticket(ticketCount);
        ticketCount --;
        System.out.println(Thread.currentThread().getName() + ticket);
    }

    public  static void sellTikct6() {
        synchronized (Station.class) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (ticketCount == 0) {
                System.out.println("没票啦！");
                return;
            }

            Ticket ticket = new Ticket(ticketCount);
            ticketCount --;
            System.out.println(Thread.currentThread().getName() + ticket);
        }
    }


}
